package com.llsydn.ui.component.alert;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
import com.llsydn.hello.R;

import java.util.Calendar;

/**
 * @author lilinshen
 * @title AlertDialog测试
 * @description 请填写相关描述
 * @date 2018/9/20 15:15
 */
public class AlertComponentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert_component);
    }

    /**
     * 1.显示一般的AlertDialog
     *
     * @param view
     */
    public void showAD(View view) {
        new AlertDialog.Builder(this)
                .setTitle("删除数据")
                .setMessage("你确定删除数据吗？")
                .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(AlertComponentActivity.this, "确定删除数据", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(AlertComponentActivity.this, "取消删除数据", Toast.LENGTH_SHORT).show();
                    }
                })
                .show();
    }

    /**
     * 2.显示单选列表的AlertDialog
     *
     * @param view
     */
    public void showLD(View view) {
        String[] items = {"红", "蓝", "绿", "灰"};

        new AlertDialog.Builder(this)
                .setTitle("指定背景颜色")
                .setSingleChoiceItems(items, 2, (dialog, which) -> {
                    //显示颜色
                    Toast.makeText(AlertComponentActivity.this, items[which], Toast.LENGTH_SHORT).show();
                    //移除dialog
                    dialog.dismiss();
                })
                .show();
    }

    /**
     * 3.显示自定义的AlertDialog
     *
     * @param view
     */
    public void showCD(View view) {
        //动态加载布局文件，得到对应的View对象
        View createView = View.inflate(this, R.layout.dialog_view, null);

        //问题1.view的真是类型？    是布局文件根标签的类型，包含了子view对象
        //问题2.如何得到一个独立view的子view    createView.findViewById()，findViewById是在setContentView()中的View中找
        EditText edt_username = createView.findViewById(R.id.edt_username);
        EditText edt_password = createView.findViewById(R.id.edt_password);

        new AlertDialog.Builder(this)
                .setView(createView)
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", (dialog, which) -> {
                    //读取用户名和密码
                    String username = edt_username.getText().toString();
                    String password = edt_password.getText().toString();
                    //提示
                    Toast.makeText(AlertComponentActivity.this, username + " : " + password, Toast.LENGTH_SHORT).show();
                })
                .show();
    }

    /**
     * 4.显示图形进度的ProgressDialog
     *
     * @param view
     */
    public void showPD(View view) {
        ProgressDialog dialog = ProgressDialog.show(this, "数据加载", "数据加载中...");

        //长时间的工作不能在主线程做，得启动分线程完成
        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 20; i++) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //移除dialog
                dialog.dismiss();//方法在分线程执行，但内部使用Handler实现主线程移除dialog

                //不能在分线程中更新ui
                //显示Toast
                //Toast.makeText(AlertComponentActivity.this, "加载完成了!", Toast.LENGTH_SHORT).show();

                runOnUiThread(new Runnable() {
                    //Runnable的run方法在主线程执行
                    @Override
                    public void run() {
                        Toast.makeText(AlertComponentActivity.this, "加载完成了!", Toast.LENGTH_SHORT).show();
                    }
                });
                //runOnUiThread()在分线程执行
            }
        }.start();

    }

    /**
     * 5.显示水平进度的ProgressDialog
     *
     * @param view
     */
    public void showPD2(View view) {
        //1.创建dialog对象
        ProgressDialog progressDialog = new ProgressDialog(this);
        //2.设置样式
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        //3.显示
        progressDialog.show();
        //4.启动分线程，加载数据，并显示进度
        new Thread(new Runnable() {
            @Override
            public void run() {
                int count = 100;
                //设置最大进度
                progressDialog.setMax(count);
                for (int i = 0; i < count; i++) {
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    progressDialog.setProgress(progressDialog.getProgress() + 1);
                }
                //移除dialog
                progressDialog.dismiss();
            }
        }).start();

    }

    /**
     * 6.显示DatePickerDialog
     */
    public void showDD(View view) {
        //创建日历对象
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Log.e("TAG", year + ":" + month + ":" + day);
            }
        }, year, month, day).show();
    }

    /**
     * 7.显示TimePickerDialog
     */
    public void showTD(View view) {
        //创建日历对象
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Log.e("TAG", hour + "::" + minute);
            }
        }, hour, minute, true).show();
    }


}
